package com.algorithm;

import java.util.HashMap;
import java.util.Map;

/**
 * 两数之和
 */
public class TwoNumbersSum {

    public static int[] twoSum(int[] nums, int target) {
        Map<Integer,Integer> map= new HashMap();
        for (int i = 0; i < nums.length; i++) {
            int res = target - nums[i];
            if (map.get(res)!=null){
                return new int[]{i,map.get(res)};
            }else {
                map.put(nums[i],i);
            }
        }
        return null;
    }

    public static void main(String[] args) {
        int [] nums={1,2,3,4,5};
        int target=7;
        int[] ints = twoSum(nums, target);
        System.out.println(ints);
    }

}
